package com.lifeng.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;

/**
 * Created by lifeng on 2019/3/27.
 */
@RestController
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/getUser")
    public String getUser(){
        return (String)redisTemplate.opsForValue().get("name");
    }

    @RequestMapping("/setUser")
    public String setUser(String name){
        redisTemplate.opsForValue().set("name",name);
        return "name:"+name;
    }

    @RequestMapping("/add")
    public String add(String name){
        Long num = redisTemplate.opsForValue().increment("serialno",1);
        redisTemplate.opsForZSet().add("merak:order:expire",1111111,123);
        double endTimes = System.currentTimeMillis() - Long.parseLong(30*60*1000+"");
        Set<Long> orders = redisTemplate.opsForZSet().rangeByScore("merak:order:expire",1.0,endTimes);
        log.info("serialno:"+num);
        return "serialno:"+num;
    }
}
